﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WilkeService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class Srv_Wilke : ISrv_Wilke
    {
       Cls_Group group;
        Cls_GroupCollection groupColl;
        Cls_UserCollection userColl;
        Cls_ProgramCollection programColl;

        public Srv_Wilke()
        {
            groupColl = new Cls_GroupCollection();
            userColl = new Cls_UserCollection();
            programColl = new Cls_ProgramCollection();

            group = new Cls_Group("NewUsers", "");
        }

        public bool ChangeSupervisorPassword(string oldPassword, string newPassword)
        {
            if (oldPassword == GetCurrentPassword())
            {
               groupColl.SupervisorPassword = newPassword;
               return true;
            }
            else
            {
                return false;
            }
        }

        public string GetCurrentPassword()
        {
            return groupColl.GetCurrentPassword();
        }

        public string ReadMessageOnClient(string message, string groupName)
        {
            foreach (var g in groupColl.ListGroups)
            {
                if (g.Message == message && g.GroupName == groupName)
                {
                    return message;
                }
            }

            return "";
        }

        public bool ChangeGroupMessage(string groupName, string message)
        {
            bool changed = false;

            foreach (Cls_Group g in groupColl.ListGroups)
            {
                if (g.GroupName == groupName)
                {
                    try
                    {
                        g.Message = message;
                        changed = true;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

            return changed;
        }

        public bool AddProgramToGroup(string groupName, string path, string programName)
        {
            bool added = false;

            foreach (Cls_Group g in groupColl.ListGroups)
            {
                if (g.GroupName == groupName)
                {
                    try
                    {
                        
                        g.AddProgramToGroup(programName, path);

                        added = true;
                    }
                    catch (Exception ex)
                    {
                        throw ex;

                    }
                }
            }
            return added;
        }

        public bool SetupNewGroup(string groupName)
        {
            bool created = false;

            try
            {
                group = new Cls_Group(groupName, "");
                groupColl.ListGroups.Add(group);
                created = true;

            }

            catch (Exception ex)
            {
                throw ex;
            }

            return created;
        }



        public List<Cls_Program> GetGroupPrograms(string groupName)
        {
            List<Cls_Program> listPrograms = null;

            foreach (Cls_Group g in groupColl.ListGroups)
            {
                if (g.GroupName == groupName)
                    listPrograms = g.ProColl.GetListPrograms;
            }
            return listPrograms;
        }


        public void ClientCheckGroup(string ip)
        {
            foreach (var user in userColl.GetListUsers)
            {
                if (user.Ip == ip)
                {
                    group.UserColl.GetListUsers.Add(user);
                }
            }
        }
    }
}
